package cn.zh.npro.Activity;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.shenyuanqing.zxingsimplify.zxing.Activity.CaptureActivity;

import cn.zh.npro.Item.CfItem;
import cn.zh.npro.MyApplication;
import cn.zh.npro.R;
import cn.zh.npro.Util.ToastUtil;

public class ORorder extends BaseActivity {
     ImageView gd_back;
     LinearLayout cclin,sclin,addlin;
     TextView clitxt;
     CfItem cfItem=null;
     MyApplication mapp;
     private static final int REQUEST_SCAN = 1;
     String key="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ororder);
        mapp= (MyApplication) getApplication();

        initor();
         }


    public void initor(){
        gd_back=findViewById(R.id.gd_back);
        clitxt=findViewById(R.id.clitxt);
        cclin=findViewById(R.id.cclin);
        sclin=findViewById(R.id.sclin);
        addlin=findViewById(R.id.addlin);
        gd_back.setOnClickListener(this);
        cclin.setOnClickListener(this);
        sclin.setOnClickListener(this);
        addlin.setOnClickListener(this);
       }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.gd_back:
                finish();
                break;
            case R.id.cclin:

                Intent intent=new Intent(ORorder.this,CliActivity.class);
                startActivityForResult(intent,0);

                break;
            case R.id.sclin:
              if (null==cfItem){
                  ToastUtil.showLong(ORorder.this,"请选择客户");
                  return;
                  }
                getRuntimeRight();

                break;
            case R.id.addlin:
                if (null==cfItem){
                    ToastUtil.showLong(ORorder.this,"请选择客户");
                    return;
                  }

                mapp.setClID(cfItem.getId());

                Intent intent1=new Intent(ORorder.this,RepOd.class);
                intent1.putExtra("type","0");
                intent1.putExtra("ClID",id);
                startActivity(intent1);
                break;
            }
         }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null==data){
            return;
         }
        if (requestCode==0){
            cfItem= (CfItem) data.getSerializableExtra("clId");
            clitxt.setText(cfItem.getCompanyName());
           }
        if(requestCode == REQUEST_SCAN && resultCode == RESULT_OK) {
            key=data.getStringExtra("barCode");
            mapp.setClID(cfItem.getId());
            Intent intent1=new Intent(ORorder.this,RepOd.class);
            intent1.putExtra("type","3");
            intent1.putExtra("key",key);

            startActivity(intent1);
           }
         
       }


    /**
     * 获得运行时权限
     */
    private void getRuntimeRight() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
        } else {
            jumpScanPage();
        }
    }

    /**
     * 跳转到扫码页
     */
    private void jumpScanPage() {
        startActivityForResult(new Intent(this, CaptureActivity.class),REQUEST_SCAN);
      }
    
    
    
    
   }
